package dabs.engine.plant;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import dabs.engine.DummyInterface;

public class ValveTest {

	DummyInterface dinterface;
	Valve valve1;
	Valve valve2;
	
	
	@Before
	public void setUp() throws Exception {
		dinterface = new DummyInterface();
		valve1 = new Valve(dinterface,"valve1",true,100,20);
		valve2 = new Valve(dinterface,"valve1",true,100,20);
	}

	
	@Test
	public void testSetIsOpen() {
		assertTrue(valve1.getIsOpen());
		valve1.setIsOpen(false);
		assertFalse(valve1.getIsOpen());		
	}
	
	@Test
	public void testNotifications(){
		valve1.setIsOpen(false);
		assertTrue(dinterface.get().getDescription().contains("Closed"));
		valve1.setIsOpen(true);
		assertTrue(dinterface.get().getDescription().contains("Opened"));
		valve1.setDeactivated(true);
		valve1.setIsOpen(false);
		assertTrue(dinterface.get().getDescription().contains("blocked"));	
	}
	
	@Test
	public void testIfClosedValvesStopFlow(){
		valve1.setOutputs(valve2);
		valve1.setIsOpen(false);
		valve1.getNewHydroState().receive(100, true);
		valve1.tick();
		valve2.tick();
		
		assertEquals(valve2.getOldHydroState().getWaterLevel(),0);
		valve1.setIsOpen(true);
		valve1.tick();
		valve2.tick();
		System.out.println(valve1.read());
		System.out.println(valve2.read());
		assertFalse(valve2.getOldHydroState().getWaterLevel()==0);
	}
	
}
